<template>
    <div class="comment-list">
        <van-cell>全部评论</van-cell>
        <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
        >
        <comment-item 
            v-for="(item,index) in list" 
            :key="index" :title="item.content" 
            :comment="item"
            @reply-click="$emit('reply-click',$event)"
        />

</van-list>
    </div>
</template>
<script>

import { getCommments } from '@/api/comment'
import CommentItem from './comment-item.vue'

export default {
    data() {
        return {
            //list: [],
            loading: false,
            finished: false,
            offset:null,
            limit:10
        }
    },
    methods: {
        async onLoad() {
            
            const { data } = await getCommments({
                type:this.type,
                source:this.source.toString(),
                offset:this.offset,
                limit:this.limit
            })
            this.$emit('update-total-count',data.data.total_count)
            //console.log(data)
            const { results } = data.data
            this.list.push(...results)


            this.loading = false;
            
            if(results.length){

                this.offset = data.data.last_id
            }else{
                this.finished = true;
            }
        }
    },
    props:{
        source:{
            type:[Number,String,Object],
            required:true
        },
        list:{
            type:Array,
            default:function(){
                return []
            }
        },
        type:{
            type:String,
            default:'a'
        }
    },
    components:{
        CommentItem
    }
}
</script>
<style lang="less" scoped>

</style>